//package com.yy.ws;
//
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.yy.Utils.MessageUtils;
//import com.yy.pojo.Message;
//import org.apache.logging.log4j.LogManager;
//import org.apache.logging.log4j.Logger;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.http.HttpSession;
//import javax.websocket.*;
//import javax.websocket.server.ServerEndpoint;
//import java.io.IOException;
//import java.util.Map;
//import java.util.Set;
//import java.util.concurrent.ConcurrentHashMap;
//
///**
// * @author 鱼凫
// * @version 1.0
// * Create by 2021/12/16 15:02
// */
//
//@ServerEndpoint(value = "/chat", configurator = GetHttpSessionConfigurator.class)
//@Component
//public class ChatEndpoint {
//    private final static Logger LOGGER = LogManager.getLogger(ChatEndpoint.class);
//
//    public static Map<String, ChatEndpoint> onlineUsers = new ConcurrentHashMap<>();
//
//    private Session session;
//
//    private HttpSession httpSession;
//
//    @OnOpen
//    public void onOpen(Session session, EndpointConfig endpointConfig) {
//        LOGGER.info("wbsocket建立连接onOpen！");
//        this.session = session;
//        //获取httpsession对象
//        HttpSession httpSession = (HttpSession) endpointConfig.getUserProperties().get(HttpSession.class.getName());
//        this.httpSession = httpSession;
//        //获取httpsession域中存放的username对应的值
//        String username = (String) httpSession.getAttribute("username");
//        //存放到onlineUsers中保存
//        onlineUsers.put(username, this);
//        //系统消息推送所有在线用户给客户端
//        //封装系统推送消息,前端onmessage接收的数据
//        String message = MessageUtils.getMessage(true, null, getAllOnlineUsername());
//        broadcastMsgToAllOnlineUsers(message);
//    }
//
//    private void broadcastMsgToAllOnlineUsers(String message) {
//        //所有登录用户名称
//        Set<String> names = onlineUsers.keySet();
//        for (String name : names) {
//            ChatEndpoint chatEndpoint = onlineUsers.get(name);
//            //获取推送对象
//            RemoteEndpoint.Basic basicRemote = chatEndpoint.session.getBasicRemote();
//            try {
//                basicRemote.sendText(message);
//            } catch (IOException e) {
//                LOGGER.error("广播发送系统消息失败！{}", e);
//                e.printStackTrace();
//            }
//        }
//    }
//
//    private Object getAllOnlineUsername() {
//        return ChatEndpoint.onlineUsers.keySet();
//    }
//
//    @OnMessage
//    public void onMessage(String message, Session session) {
//        ObjectMapper objectMapper = new ObjectMapper();
//        try {
//            Message msg = objectMapper.readValue(message, Message.class);
//            String toName = msg.getToName();
//            String msgData = msg.getMessage();
//            String username = (String) httpSession.getAttribute("username");
//            String sendMsg = MessageUtils.getMessage(false, username, msgData);
//            onlineUsers.get(toName).session.getBasicRemote().sendText(sendMsg);
//        } catch (JsonProcessingException e) {
//            LOGGER.error("接收客户端的消息，转换出错了！");
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**关闭时调用*/
//    @OnClose
//    public void onClose(Session session) {
//        String username = (String) httpSession.getAttribute("username");
//        ChatEndpoint remove = onlineUsers.remove(username);
//        String message = MessageUtils.getMessage(true, null, getAllOnlineUsername());
//        broadcastMsgToAllOnlineUsers(message);
//    }
//
//}
